package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.ContactorMapper;
import com.example.pojo.dto.ContactorPageDTO;
import com.example.pojo.entity.Contactor;
import com.example.result.PageResult;
import com.example.service.ContactorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContactorServiceImpl extends ServiceImpl<ContactorMapper, Contactor> implements ContactorService {

    @Autowired
    private ContactorMapper contactorMapper;

    @Override
    public void updateContactor(Contactor contactor) {
        QueryWrapper<Contactor> wrapper = new QueryWrapper<>();
        wrapper.eq("id",contactor.getId());
        this.update(contactor,wrapper);
    }

    @Override
    public PageResult pageQuery(ContactorPageDTO contactorPageDTO) {
        Integer pageSize = contactorPageDTO.getPageSize();
        Integer pageNum = contactorPageDTO.getPageNum();
        String name = contactorPageDTO.getName();

        // 查询条件
        QueryWrapper<Contactor> wrapper = new QueryWrapper<>();
        wrapper.like(name != null && !name.isEmpty(),"name",name);

        // 设置分页参数
        Page<Contactor> page = Page.of(pageNum, pageSize);
        // 分页查询
        Page<Contactor> p = page(page,wrapper);
        
        // 解析数据
        List<Contactor> records = p.getRecords();
        long total = p.getTotal();

        return new PageResult(total,records);
    }


}
